package controller;

import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.RuntimeExceptionDao;

import DB.DatabaseHelper;
import android.content.Context;
import model.ConsumoDiario;
import model.RegimenAlimenticio;

public class ConsumoDiarioController {
	private static ConsumoDiarioController instance = null;
	protected ConsumoDiarioController() {
	}
	public static ConsumoDiarioController getInstance() {
		if(instance == null) {
    	  instance = new ConsumoDiarioController();
		}
		return instance;
	}
	public void index(){
		
	}
	
	public ConsumoDiario add(Context context){
		ConsumoDiario consumoDiario=null;
		DatabaseHelper db =new DatabaseHelper(context);
		try {
			RuntimeExceptionDao<RegimenAlimenticio, Integer> regimenDao = db.getRegimenAlimenticioDaoException();
			RegimenAlimenticio info = regimenDao.queryBuilder().orderBy("id", false).queryForFirst();
			if(info!=null){
				Dao<ConsumoDiario, Integer> consumoDiarioDao = db.getConsumoDiarioDao();
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				Date hoy = sdf.parse(sdf.format(new Date()));
				consumoDiario=new ConsumoDiario();
				consumoDiario.setDia(hoy);
				consumoDiario.setPuedoConsumir(info);
				consumoDiarioDao.create(consumoDiario);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return consumoDiario;
		
	}
	public void update(){
	
	}
	public void delete(){
		
	}
}